package com.lq.dubbo.rocketmq.shop.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.lq.dubbo.rocketmq.shop.api.IUserService;
import com.lq.dubbo.rocketmq.shop.constant.ShopCode;
import com.lq.dubbo.rocketmq.shop.entity.Result;
import com.lq.dubbo.rocketmq.shop.exception.CastException;
import com.lq.dubbo.rocketmq.shop.mapper.TradeUserMapper;
import com.lq.dubbo.rocketmq.shop.mapper.TradeUserMoneyLogMapper;
import com.lq.dubbo.rocketmq.shop.shop_pojo.TradeUser;
import com.lq.dubbo.rocketmq.shop.shop_pojo.TradeUserMoneyLog;
import com.lq.dubbo.rocketmq.shop.shop_pojo.TradeUserMoneyLogExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @ClassName UserServiceImpl
 * @Description
 * @Author LQ
 * @Date 2021/6/5 17:30
 * @Version 1.0
 */
@Service(interfaceClass = IUserService.class)
@Component
public class UserServiceImpl implements IUserService {

    @Autowired
    TradeUserMapper userMapper;
    @Autowired
    private TradeUserMoneyLogMapper userMoneyLogMapper;

    @Override
    public TradeUser findOne(Long userId) {
        if (userId == null) {
            CastException.cast(ShopCode.SHOP_REQUEST_PARAMETER_VALID);
        }
        TradeUser user = userMapper.selectByPrimaryKey(userId);
        return user;
    }

    @Override
    public Result updateMoneyPaid(TradeUserMoneyLog userMoneyLog) {
        // 1.校验参数是否合法
        if (userMoneyLog == null ||
                userMoneyLog.getOrderId() == null ||
                userMoneyLog.getUserId() == null ||
                userMoneyLog.getUseMoney() == null ||
                userMoneyLog.getUseMoney().compareTo(BigDecimal.ZERO) <= 0
        ) {
            CastException.cast(ShopCode.SHOP_REQUEST_PARAMETER_VALID);
        }
        //2. 查询订单余额使用日志
        TradeUserMoneyLogExample userMoneyLogExample = new TradeUserMoneyLogExample();
        TradeUserMoneyLogExample.Criteria criteria = userMoneyLogExample.createCriteria();
        criteria.andOrderIdEqualTo(userMoneyLog.getOrderId());
        criteria.andUserIdEqualTo(userMoneyLog.getUserId());
        long r = userMoneyLogMapper.countByExample(userMoneyLogExample);
        TradeUser tradeUser = userMapper.selectByPrimaryKey(userMoneyLog.getUserId());
        // 3.操作余额
        if (userMoneyLog.getMoneyLogType().intValue() == ShopCode.SHOP_USER_MONEY_PAID.getCode().intValue()) {
            // 3.1 付款,扣减余额
            if (r > 0) {
                CastException.cast(ShopCode.SHOP_ORDER_PAY_STATUS_IS_PAY);
            }
            // 扣减余额
            tradeUser.setUserMoney(tradeUser.getUserMoney().subtract(userMoneyLog.getUseMoney()));
            userMapper.updateByPrimaryKey(tradeUser);
        } else if (userMoneyLog.getMoneyLogType().intValue() == ShopCode.SHOP_USER_MONEY_REFUND.getCode().intValue()) {
            //3.2 退款,回退余额
            if (r <= 0) {// ??不是小于1??
                // 如果没有支付,则不能回退余额
                CastException.cast(ShopCode.SHOP_ORDER_PAY_STATUS_NO_PAY);
            }
            // 防止多次退款
            TradeUserMoneyLogExample userMoneyLogExample2 = new TradeUserMoneyLogExample();
            TradeUserMoneyLogExample.Criteria criteria1 = userMoneyLogExample2.createCriteria();
            criteria1.andOrderIdEqualTo(userMoneyLog.getOrderId());
            criteria1.andUserIdEqualTo(userMoneyLog.getUserId());
            criteria1.andMoneyLogTypeEqualTo(ShopCode.SHOP_USER_MONEY_REFUND.getCode());
            long r2 = userMoneyLogMapper.countByExample(userMoneyLogExample2);
            if (r2 > 0) {
                CastException.cast(ShopCode.SHOP_USER_MONEY_REFUND_ALREADY);
            }
            tradeUser.setUserMoney(tradeUser.getUserMoney().add(userMoneyLog.getUseMoney()));
            userMapper.updateByPrimaryKey(tradeUser);
        }
        //4.记录订单余额使用日志
        userMoneyLog.setCreateTime(new Date());
        int insert = userMoneyLogMapper.insert(userMoneyLog);
        return new Result(ShopCode.SHOP_SUCCESS.getSuccess(), ShopCode.SHOP_SUCCESS.getMessage());
    }
}
